﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using ROMAP.Utilities;
using FlatRedBall.Graphics.Animation;

namespace ROMAP.Entities.Tiles
{
    public class WinTile : Tile
    {
        #region Fields
        private bool _opened;
        private bool _playerIsClose;
        private bool _startOpenAni; //Reset the open portal animation to the first frame
        private bool _startCloseAni;

        #endregion

        #region Properties

        #endregion

        #region Methods

        public WinTile(int x, int y)
        {
            TilePosition = new Vector2i(x, y);
            Initialize();
        }

        protected override void Initialize()
        {
            InitializeAnimations();

            _sprite.AttachTo(this, false);
            _sprite.PixelScale();
            _sprite.ScaleX *= .5f;
            _sprite.ScaleY *= .5f;

            _sprite.RelativeZ = 0.05f;

            _collision = ShapeManager.AddAxisAlignedRectangle();
            _collision.AttachTo(this, false);
            _collision.ScaleX = _sprite.ScaleX * 0.6f;
            _collision.ScaleY = _sprite.ScaleY;
            _collision.Visible = false;

            _opened = false;
            _playerIsClose = false;
            _startOpenAni = true;
            _startCloseAni = false;

            //base.Initialize();
        }

        public override void Activity()
        {
            if (_playerIsClose)
            {
                _startCloseAni = true;

                if (!_opened)
                {
                    if (_startOpenAni)
                    {
                        _sprite.CurrentFrameIndex = 0;
                        _startOpenAni = false;
                    }
                    _sprite.CurrentChainName = "open";
                    _sprite.Animate = true;
                }
                else
                    _sprite.CurrentChainName = "portalLoop";

                if (_sprite.JustCycled)
                {
                    _opened = true;
                    _startOpenAni = true;
                }
            }
            else
            {
                if (_startCloseAni)
                {
                    _sprite.CurrentFrameIndex = 0;
                    _startCloseAni = false;
                }
                _sprite.CurrentChainName = "close";
                if (_sprite.CurrentChainName.Equals("close") && _sprite.JustCycled)
                {
                    _sprite.CurrentFrameIndex = 4;
                    _sprite.Animate = false;
                    _opened = false;
                }
            }

            //when "open" animation completes, start the "portalLoop" animation
        }

        public virtual void Destroy()
        {
            base.Destroy();
        }

        private void InitializeAnimations()
        {
            // Loads the animation chain for when the buckle opens.
            AnimationChainList ani = new AnimationChainList();
            AnimationChain open = new AnimationChain();
            AnimationChain close = new AnimationChain();
            AnimationChain portalLoop = new AnimationChain();

            for (int i = 1; i <= 5; i++)
                open.Add(new AnimationFrame(@"Content/Obstacles/WinPortal/" + i, 0.06f, "Global"));
            for (int i = 5; i >= 1; i--)
                close.Add(new AnimationFrame(@"Content/Obstacles/WinPortal/" + i, 0.06f, "Global"));
            for (int i = 1; i <= 3; i++)
                portalLoop.Add(new AnimationFrame(@"Content/Obstacles/WinPortal/GreenLoop/" + i, 0.06f, "Global"));

            open.Name = "open";
            close.Name = "close";
            portalLoop.Name = "portalLoop";

            ani.Add(open);
            ani.Add(close);
            ani.Add(portalLoop);


            _sprite = SpriteManager.AddSprite(ani);
            _sprite.CurrentChainName = "close";
            _sprite.CurrentFrameIndex = 4;
            _sprite.Animate = false;
            _sprite.PixelScale();
            _sprite.ScaleX *= .5f;
            _sprite.ScaleY *= .5f;
            _sprite.AttachTo(this, false);
        }

        public void PlayerIsClose(bool isClose)
        {
            _playerIsClose = isClose;
        }
        #endregion
    }
}
